package com.geeklan.smart.ui.feed;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;

import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.common.Constants;
import com.geeklan.smart.data.model.FeedRule;
import com.geeklan.smart.databinding.ActivityFeedingPlanBinding;
import com.geeklan.smart.ui.feed.fragment.FeedingPlanFragment;
import com.geeklan.smart.ui.feed.fragment.FeedingRuleFragment;
import com.geeklan.smart.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class FeedPlanActivity extends BaseActivity<ActivityFeedingPlanBinding> implements CompoundButton.OnCheckedChangeListener {
    private FeedingPlanFragment feedingPlanFragment;
    private FeedingRuleFragment feedingRuleFragment;
    private List<DateUtils.DateAndWeek> datesAndWeeks;
    private String deviceId;
    private ArrayList<FeedRule> timedPlans = new ArrayList<>();
    private boolean isInit = false;
    public void setTimedPlans(ArrayList<FeedRule> timedPlans) {
        isInit = true;
        this.timedPlans = timedPlans;
    }

    @Override
    protected ActivityFeedingPlanBinding initBinding() {
        return ActivityFeedingPlanBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        deviceId = getIntent().getStringExtra("deviceId");
        datesAndWeeks = DateUtils.getTodayAndNextDates(6);
    }

    @Override
    protected void initView() {
        mBinding.tvToday.setText("今日 " + DateUtils.dateFormatDay2(datesAndWeeks.get(0).getDay()) + " " + Constants.WEEKDAYS1[datesAndWeeks.get(0).getWeek()]);
        feedingPlanFragment = new FeedingPlanFragment();
        feedingPlanFragment.setDatesAndWeeks(datesAndWeeks);
        feedingRuleFragment = new FeedingRuleFragment();
        replaceFragment(feedingPlanFragment);
        mBinding.rbFeedPlan.setOnClickListener(v -> {
            selectFragment(feedingPlanFragment);
            replaceFragment(feedingPlanFragment);
        });
        mBinding.rbFeedRule.setOnClickListener(v -> {
            selectFragment(feedingRuleFragment);
            replaceFragment(feedingRuleFragment);
        });
        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });

        mBinding.radio1.setText(DateUtils.dateFormatDay(datesAndWeeks.get(0).getDay()) + "\n" + Constants.WEEKDAYS1[datesAndWeeks.get(0).getWeek()]);
        mBinding.radio2.setText(DateUtils.dateFormatDay(datesAndWeeks.get(1).getDay()) + "\n" + Constants.WEEKDAYS1[datesAndWeeks.get(1).getWeek()]);
        mBinding.radio3.setText(DateUtils.dateFormatDay(datesAndWeeks.get(2).getDay()) + "\n" + Constants.WEEKDAYS1[datesAndWeeks.get(2).getWeek()]);
        mBinding.radio4.setText(DateUtils.dateFormatDay(datesAndWeeks.get(3).getDay()) + "\n" + Constants.WEEKDAYS1[datesAndWeeks.get(3).getWeek()]);
        mBinding.radio5.setText(DateUtils.dateFormatDay(datesAndWeeks.get(4).getDay()) + "\n" + Constants.WEEKDAYS1[datesAndWeeks.get(4).getWeek()]);
        mBinding.radio6.setText(DateUtils.dateFormatDay(datesAndWeeks.get(5).getDay()) + "\n" + Constants.WEEKDAYS1[datesAndWeeks.get(5).getWeek()]);
        mBinding.radio7.setText(DateUtils.dateFormatDay(datesAndWeeks.get(6).getDay()) + "\n" + Constants.WEEKDAYS1[datesAndWeeks.get(6).getWeek()]);

        mBinding.radio1.setOnCheckedChangeListener(this);
        mBinding.radio2.setOnCheckedChangeListener(this);
        mBinding.radio3.setOnCheckedChangeListener(this);
        mBinding.radio4.setOnCheckedChangeListener(this);
        mBinding.radio5.setOnCheckedChangeListener(this);
        mBinding.radio6.setOnCheckedChangeListener(this);
        mBinding.radio7.setOnCheckedChangeListener(this);
        mBinding.ivAdd.setOnClickListener(v -> {
            if(!isInit) return;
            EditFeedPlanActivity.startActivity(this,deviceId,timedPlans);
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked && feedingPlanFragment != null && feedingPlanFragment.isAdded()) {
            int id = buttonView.getId();
            if (id == mBinding.radio1.getId()) {
                feedingPlanFragment.checkDate(datesAndWeeks.get(0).getWeek());
            } else if (id == mBinding.radio2.getId()) {
                feedingPlanFragment.checkDate(datesAndWeeks.get(1).getWeek());
            } else if (id == mBinding.radio3.getId()) {
                feedingPlanFragment.checkDate(datesAndWeeks.get(2).getWeek());
            } else if (id == mBinding.radio4.getId()) {
                feedingPlanFragment.checkDate(datesAndWeeks.get(3).getWeek());
            } else if (id == mBinding.radio5.getId()) {
                feedingPlanFragment.checkDate(datesAndWeeks.get(4).getWeek());
            } else if (id == mBinding.radio6.getId()) {
                feedingPlanFragment.checkDate(datesAndWeeks.get(5).getWeek());
            } else if (id == mBinding.radio7.getId()) {
                feedingPlanFragment.checkDate(datesAndWeeks.get(6).getWeek());
            }
        }
    }

    private void selectFragment(Fragment fragment) {
        if (fragment instanceof FeedingPlanFragment) {
            mBinding.clDate.setVisibility(View.VISIBLE);
            mBinding.ivAdd.setVisibility(View.INVISIBLE);
            mBinding.clTitle.setBackground(AppCompatResources.getDrawable(this, R.drawable.layout_white_bottom_radius_dp10));
        } else if (fragment instanceof FeedingRuleFragment) {
            mBinding.clDate.setVisibility(View.GONE);
            mBinding.ivAdd.setVisibility(View.VISIBLE);
            mBinding.clTitle.setBackground(null);
            mBinding.clTitle.setBackground(AppCompatResources.getDrawable(this, com.smart.base.R.color.white));
        }
    }

    // 替换 Fragment 的方法
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.flFragment, fragment);
        transaction.commit();
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    public static void startActivity(Context context,String deviceId) {
        Intent intent = new Intent(context, FeedPlanActivity.class);
        intent.putExtra("deviceId",deviceId);
        context.startActivity(intent);
    }
}